﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;
using XZL.GM.Entity;

public partial class User_UserManageEdit : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitMenuList();
            InitStatus();
            BindUserInfo();
        }
    }

    private void InitStatus()
    {
        ddlStatus.DataSource = UserInfoBiz.GetUserStatusList();
        ddlStatus.DataTextField = "StatusName";
        ddlStatus.DataValueField = "StatusID";
        ddlStatus.DataBind();
    }

    /// <summary>
    /// 初始化权限列表
    /// </summary>
    private void InitMenuList()
    {
        litMenuList.Text = "";
        List<Proc_MenuManage_SelectAllResult> menuList = UserInfoBiz.GetAllMenuList();
        for (int i = 0; i < menuList.Count; i++)
        {
            if (menuList[i].ParentID == 0)
            {
                if (i == 0)
                {
                    litMenuList.Text += "<b>" + menuList[i].MenuName + "：</b><br />";
                }
                else
                {
                    litMenuList.Text += "<br/><br/><b>" + menuList[i].MenuName + "：</b><br />";
                }
            }
            else
            {
             litMenuList.Text += "<label style=\" color:#42464f;font-size:15px; \"><input type=\"checkbox\" onclick=\"GetMenuIDString()\"  value=" + menuList[i].MenuID + "></input>" + menuList[i].MenuName + "</label>&nbsp;&nbsp;";
            }
        }
    }

    /// <summary>
    /// 绑定管理员信息
    /// </summary>
    private void BindUserInfo()
    {
        string str = Request.QueryString["UserID"];
        if (str != null)
        {
            string userID = Convert.ToString(str);
            Proc_UserInfo_SelectUserInfoResult user = UserInfoBiz.GetUserInfo(userID);
            Proc_UserRole_SelectResult role = UserInfoBiz.GetUserRole(userID);

            txtUserName.Text = user.UserName;
            txtUserName.Enabled = false;        //登录名将不可修改（不启用控件）

            divPwd1.Visible = false;           //密码不显示在页面上
            divPwd2.Visible = false;           //确认密码不显示在页面上

            
            txtNickName.Text = user.NickName;
            txtTelphone.Text = user.MobilePhone;
         
            ddlStatus.SelectedValue = user.Status.ToString();
            
           ClientScript.RegisterStartupScript(this.GetType(), "", "BindMenuList('" + role.MenuList + "')", true);
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {

        string str = Request.QueryString["UserID"];

        //验证登录名
        if (txtUserName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入您的登录名！";
            txtUserName.Focus();
            return;
        }

        //验证姓名
        if (txtNickName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入您的账户名或昵称！";
            txtNickName.Focus();
            return;
        }

        //验证手机号
        if (txtTelphone.Text.Trim().Length != 11)
        {
            lblWarn.Text = "请输入正确的手机号！";
            txtTelphone.Focus();
            return;
        }

      
      
        int status = Convert.ToInt32(ddlStatus.SelectedValue);

        //添加页面
        if (str == null)
        {
            //验证密码1
            if (txtPwd1.Text.Trim().Length < 6 || txtPwd1.Text.Trim().Length > 20)
            {
                lblWarn.Text = "请输入6-20位密码！";
                txtPwd1.Focus();
                return;
            }

            //验证确认密码
            if (txtPwd2.Text.Trim() != txtPwd1.Text.Trim())
            {
                lblWarn.Text = "确认密码输入错误！";
                txtPwd1.Focus();
                return;
            }

            string userID = Guid.NewGuid().ToString();
            string userName = txtUserName.Text.Trim();
            string nickName = txtNickName.Text.Trim();
            string mobilePhone = txtTelphone.Text.Trim();
            string password = CommonMethod.StrToMD5(txtPwd1.Text.Trim());
            string pwdSecond = password;

            int res = UserInfoBiz.UserBackReigister(userID, userName, nickName, mobilePhone, password, 4,status);
            if (res == 0)
            {
                UserInfoBiz.UpdateUserRole(userID, lblMenuList.Value);
                Response.Redirect("UserManage.aspx");
                // lblWarn.Text = "恭喜！帐号注册成功！";
                // lblWarn.ForeColor = System.Drawing.Color.Green;
                btnOK.Enabled = false;
            }
            else if (res == -1)
            {
                lblWarn.Text = "用户名已存在，请重新输入！";
                lblWarn.Text = "";
            }

            else
            {
                lblWarn.Text = "注册失败！";
                lblWarn.ForeColor = System.Drawing.Color.Red;
            }
        }

        //编辑页面
        else
        {
            if (str != null)
            {
                string  userID = Convert.ToString(str);
                Proc_UserInfo_SelectUserInfoResult user = UserInfoBiz.GetUserInfo(userID);
                string mobilePhone = txtTelphone.Text.Trim();
                string nickName = txtNickName.Text.Trim();

                bool b = UserInfoBiz.UpdateUserInfo(userID, nickName, mobilePhone, "", "", 4, status);
                //userID, nickName, mobilePhone,weiXinName,headPortrait, userType,status
                if (b)
                {
                    UserInfoBiz.UpdateUserRole(userID, lblMenuList.Value);
                    Response.Redirect("UserManage.aspx");
                }
            }
        }

        //验证全部通过

    }
}